package com.vma.shoeSystem.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vma.offer.service.MarketService;
import com.vma.shoeSystem.util.ReturnsUtil;
import com.vma.shoes.bizModel.model.Market;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2017/4/5.
 */
public class MarketController extends Controller {

    public void add(){
        try{
            SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String marketName = getPara("marketName");
            String expDays = getPara("expDays");
            String theme = getPara("theme");
            String marketDesc = getPara("marketDesc");
            String imgUrl = getPara("imgUrl");
            Date endTime = sdFormatter.parse("3000-01-01 23:59:59");

            new Market().setCreateTime(new Date()).setExpTime(endTime).setStartTime(new Date())
                    .setImgUrl(imgUrl).setVStatus("1").setMarketName(marketName).setMarketDesc(marketDesc).setTheme(theme).save();
            renderJson(ReturnsUtil.returnSuccess("添加专场成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void qryMarketList(){
        try {
            String marketName = getPara("marketName");

            int pageNo = getParaToInt("cPage");
            int pageSize = getParaToInt("pSize");

            List<String> param = new ArrayList<String>();
            String condition = "";


            if(marketName != null && !"".equals(marketName)){
                condition += "and market_name like ? ";
                param.add("%"+marketName+"%");
            }
            Page<Market> marketPage = Market.dao.paginate(pageNo,pageSize,"select * ","from market where 1=1 "+condition+
                            " order by create_time desc"
                    ,param.toArray());
            renderJson(ReturnsUtil.returnSuccess(marketPage));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void updateStatus(){
        try{
            String id = getPara("id");
            Market market = Market.dao.findById(id);
            if (market.getVStatus().equals("1")){
                market.setVStatus("2").update();
            }else {
                market.setVStatus("1").update();
            }
            renderJson(ReturnsUtil.returnSuccess("更改状态成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void editOffers(){
        try {
            List<Record> list = MarketService.qryOfferListByMarketId(getPara("id"));
            setAttr("offers",list);
            setAttr("marketId",getPara("id"));
            renderVelocity("/shop/offer/market/addOffer.html");
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void addOfferRela(){

        try {
            String offerIds = getPara("offerIds");
            String marketId = getPara("marketId");
            MarketService.addOfferRela(offerIds,marketId);
            renderJson(ReturnsUtil.returnSuccess("更改状态成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
